from Event import Event


class Publisher:

    def __init__(self, event):
        self.__observers = []
        self.event = event

    def getEvent(self):
        return self.event

    def setEvent(self, event: Event):
        self.event = event

    # TODO:检查观察者列表是否为空
    def hasObserver(self):
        return False if not self.__observers else True

    # TODO:添加观察者
    def addObserver(self, observer):
        # 添加之前检查是否存在
        if observer not in self.__observers:
            self.__observers.append(observer)
        else:
            print("Failed to add:{}".format(observer))

    # TODO:移除观察者
    def removeObserver(self, observer):
        try:
            self.__observers.remove(observer)
        except ValueError:
            print("Failed to remove:{}".format(observer))

    # TODO:发布消息
    def notify(self):
        for observer in self.__observers:
            observer.update(self)
